cap log close 
log using "${logpath}litigation_trends.log", replace

/*******************************************************************************
litigation_trends.do

Produces figures of raw trends for districts subject to litigation
*******************************************************************************/

clear all

qui {
	use "${cleandatapath}dist_data.dta", clear
	
	format month_date %tm
	foreach var in crim civ PA {
		gen `var'date=mofd(`var'_date)
		format `var'date %tm
	}

	gen pmt_ht=tot_pmt/100000

	sort civdate month_date
	levelsof civdate, local(dates)
}

local alphabet " a b c d e f g h i j k l m n "
tokenize "`alphabet'"

local counter=1
foreach d of local dates {
	if `d'>516 & `d'<695 {
		qui su crimdate if civdate==`d'
		if `r(N)'==0 {
			local crim = 50000
		}
		if `r(N)'>0 {
			local crim =`r(mean)'
			if `crim'<516 {
				local crim = 50000
			}
		}
		qui su PAdate if civdate==`d'
		if `r(N)'==0 {
			local PA = 50000
		}
		if `r(N)'>0 {
			local PA=`r(mean)'
		}
		twoway (line pmt_ht month_date if civdate==`d'), ///
			xline(`PA') xline(`d', lc(forest_green) lpatt(-)) ///
			xline(`crim', lc(dkorange) lpatt(_)) ///
			xti(" ") yti("Monthly Ambulance Payments ($100,000)") ///
			xlab(516 "2003" 552 "2006" 588 "2009" 624 "2012" 660 "2015" 696 "2018") ///
			ylab(, format(%9.0gc)) ///
			leg(off) graphregion(color(white)) bgcolor(white)
		qui graph export "${outpath}Figure_A2``counter''.pdf", as(pdf) replace // Figure A2
		local crim = 50000
		local PA = 50000
		
		local counter=`counter'+1
	}
}

sort crimdate month_date
qui levelsof crimdate, local(dates)

local counter=1
foreach d of local dates {
	if `d'>516 & `d'<695 {
		qui su civdate if crimdate==`d'
		if `r(N)'==0 {
			local civ = 50000
		}
		if `r(N)'>0 {
			local civ=`r(mean)'
			if `civ'<516 {
				local civ = 50000
			}
		}
		qui su PAdate if crimdate==`d'
		if `r(N)'==0 {
			local PA = 50000
		}
		if `r(N)'>0 {
			local PA=`r(mean)'
		}
		twoway (line pmt_ht month_date if crimdate==`d'), ///
			xline(`PA') xline(`civ', lc(forest_green) lpatt(-)) ///
			xline(`d', lc(dkorange) lpatt(_)) ///
			xti(" ") yti("Monthly Ambulance Payments ($100,000)") ///
			xlab(516 "2003" 552 "2006" 588 "2009" 624 "2012" 660 "2015" 696 "2018") ///
			ylab(, format(%9.0gc)) ///
			leg(off) graphregion(color(white)) bgcolor(white)
		qui graph export "${outpath}Figure_A3``counter''.pdf", as(pdf) replace // Figure A3
		local civ = 50000
		local PA = 50000
		
		local counter=`counter'+1		
	}
}

log close